package com.itheima.wms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.base.BaseController;
import com.itheima.pinda.base.R;
import com.itheima.pinda.base.entity.SuperEntity;
import com.itheima.wms.dto.CheckDTO;
import com.itheima.wms.entity.CheckEntity;
import com.itheima.wms.service.CheckService;
import com.itheima.wms.service.StatusRecordService;
import com.itheima.wms.vo.BatchVO;
import com.itheima.wms.vo.CheckDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * 盘点单
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/check")
@Api(value = "Check", tags = "盘点单")
public class CheckController extends BaseController {

    @Autowired
    private CheckService checkService;

    @Autowired
    private StatusRecordService statusRecordService;

    /**
     * 分页查询盘点单
     *
     * @param data 分页查询对象
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询盘点单", notes = "分页查询盘点单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "code", value = "盘点单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "planCheckTime", value = "计划盘点时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "reason", value = "盘点原因 规划:GH、货主:HZ、差异:CY", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dimension", value = "盘点维度 库位:KW、货品:HP", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "盘点类型 随机盘点:SJPD、计划盘点:JHPD", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "库区id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "盘点单状态 1新建、2一盘中、3一盘完成、4复盘中、5复盘完成、6已取消", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/page")
    public R<IPage<CheckEntity>> page(@RequestParam Map data) {
        Page<CheckEntity> page = getPage();
        IPage<CheckEntity> checkPage = checkService.page(page, data);
        page.setRecords(checkPage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "分页查询盘点单明细", notes = "分页查询盘点单明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "code", value = "盘点单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "库区id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "ownerName", value = "货主名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "reason", value = "盘点原因 规划:GH、货主:HZ、差异:CY", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dimension", value = "盘点维度 库位:KW、货品:HP", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "盘点类型 随机盘点:SJPD、计划盘点:JHPD", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "盘点单状态 1新建、2一盘中、3一盘完成、4复盘中、5复盘完成、6已取消", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageDetail")
    public R<IPage<CheckDetailVO>> pageDetail(@RequestParam Map data) {
        Page<CheckDetailVO> page = getPage();
        IPage<CheckDetailVO> checkDetailVOIPage = checkService.pageDetail(page, data);
        page.setRecords(checkDetailVOIPage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "查询全部盘点单", notes = "查询全部盘点单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "code", value = "盘点单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "planCheckTime", value = "计划盘点时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "reason", value = "盘点原因 规划:GH、货主:HZ、差异:CY", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dimension", value = "盘点维度 库位:KW、货品:HP", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "盘点类型 随机盘点:SJPD、计划盘点:JHPD", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "库区id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "盘点单状态 1新建、2一盘中、3一盘完成、4复盘中、5复盘完成、6已取消", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/list")
    public R<List<CheckEntity>> list(@RequestParam Map data) {
        List<CheckEntity> checkArray = checkService.list(data);
        return success(checkArray);
    }

    /**
     * 查询盘点单
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询盘点单", notes = "查询盘点单")
    @GetMapping("/{id}")
    public R<CheckEntity> get(@PathVariable Long id) {
        return success(checkService.getById(id));
    }

    /**
     * 查询详情
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @GetMapping("detail/{id}")
    public R<CheckDetailVO> getDetail(@PathVariable Long id) {
        return success(checkService.getDetail(id));
    }

    /**
     * 新增盘点单
     *
     * @param data 新增对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增盘点单", notes = "新增盘点单不为空的字段")
    @PostMapping
    public R<CheckEntity> save(@RequestBody @Validated(SuperEntity.Save.class) CheckDTO data) {
        checkService.save(data);
        return success(data);
    }

    /**
     * 生成盘点任务
     *
     * @param ids 新增对象
     * @return 生成 结果
     */
    @ApiOperation(value = "生成盘点任务", notes = "")
    @PostMapping("task")
    public R<BatchVO> checkTask(@RequestBody List<Long> ids) {
        return success(checkService.task(ids));
    }

    /**
     * 修改盘点单
     *
     * @param data 修改对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改盘点单", notes = "修改盘点单不为空的字段")
    @PutMapping
    public R<CheckEntity> update(@RequestBody @Validated(SuperEntity.Update.class) CheckDTO data) {
        checkService.updateById(data);
        return success(data);
    }

    /**
     * 取消盘点单
     *
     * @return 取消结果
     */
    @ApiOperation(value = "取消盘点单", notes = "取消盘点单")
    @PutMapping("cancel")
    public R<CheckEntity> cancel(@RequestBody @Validated(SuperEntity.Update.class) CheckDTO data) {
        checkService.cancel(data);
        return success(data);
    }

    /**
     * 删除盘点单
     *
     * @param ids 主键id
     * @return 删除结果
     */
    @ApiOperation(value = "删除盘点单", notes = "根据id物理删除盘点单")
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("ids[]") List<Long> ids) {
        checkService.removeByIds(ids);
        return success();
    }

}
